<!-- 在线网页编辑器引入 -->
<link rel="stylesheet" href="/static/kindeditor/themes/default/default.css" />
<script src="/static/kindeditor/kindeditor-all-min.js"></script>
<script src="/static/kindeditor/lang/zh-CN.js"></script>
<script>
  var editor;
  KindEditor.ready(function (K) {
    editor = K.create('textarea[name="content"]', {
      allowFileManager: true
    });
  })
</script>

<!-- 二级菜单 -->
<div class="layui-hide-xs">
  <div class="fly-panel fly-column">
    <div class="layui-container">
      <ul class="layui-clear">
        <li class="{$cateid == '' ? 'layui-hide-xs layui-this' : ''}"><a href="{:url('home/index/index')}">全部</a></li>
        {foreach($cateList as $item)}
        <li class="{$cateid == $item.id ? 'layui-hide-xs layui-this' : ''}"><a
            href="{:url('home/index/index',['cateid' => $item.id])}">{$item.name}</a></li>
        {/foreach}
        <li class="layui-hide-xs layui-hide-sm layui-show-md-inline-block"><span class="fly-mid"></span></li>
        <!-- 用户登入后显示 -->
        {if condition="$Think.cookie.LoginUser"}
        <li class="layui-hide-xs layui-hide-sm layui-show-md-inline-block"><a href="{:url('home/post/index',['bg' => '2'])}">我发表的贴</a></li>
        <li class="layui-hide-xs layui-hide-sm layui-show-md-inline-block"><a
            href="{:url('home/post/collect' ,['bg' => '3'])}">我收藏的贴</a></li>
        {/if}
      </ul>
      <div class="fly-column-right layui-hide-xs">
        <span class="fly-search"><i class="layui-icon"></i></span>
        <a href="{:url('home/post/add')}" class="layui-btn">发表新帖</a>
      </div>
      <div class="layui-hide-sm layui-show-xs-block"
        style="margin-top: -10px; padding-bottom: 10px; text-align: center;">
        <a href="{:url('home/post/add')}" class="layui-btn">发表新帖</a>
      </div>
    </div>
  </div>
</div>
<div class="layui-container">
  <div class="layui-row layui-col-space15">
    <div class="layui-col-md8 content detail">
      <div class="fly-panel detail-box">
        <!-- 标题 -->
        <h1>{$Post.title}</h1>
        <!-- 帖子状态 -->
        <div class="fly-detail-info">
          <!-- <span class="layui-badge">审核中</span> -->
          <span class="layui-badge layui-bg-green fly-detail-column">{$Post.cate.name}</span>
          {if condition="$Post.accept"}
          <span class="layui-badge" style="background-color: #5FB878;">已结</span>
          {else/}
          <span class="layui-badge" style="background-color: #999;">未结</span>
          {/if}
          {if condition="$Post.state eq 1"}
          <span class="layui-badge layui-bg-blue">置顶</span>
          {elseif condition="$Post.state eq 2"}
          <span class="layui-badge layui-bg-orange">精华</span>
          {elseif condition="$Post.state eq 3"}
          <span class="layui-badge layui-bg-red">热门</span>
          {/if}
          <div class="fly-admin-box" data-id="123">
            {if condition="$Think.cookie.LoginUser['id'] == $Post.user.id"}
            <span class="layui-btn layui-btn-xs jie-admin" data-id="{$Post.id}" id="PostDel" type="del">删除</span>
            {/if}

            <!-- <span class="layui-btn layui-btn-xs jie-admin" type="set" field="stick" rank="1">置顶</span>  -->
            <!-- <span class="layui-btn layui-btn-xs jie-admin" type="set" field="stick" rank="0" style="background-color:#ccc;">取消置顶</span> -->

            <!-- <span class="layui-btn layui-btn-xs jie-admin" type="set" field="status" rank="1">加精</span>  -->
            <!-- <span class="layui-btn layui-btn-xs jie-admin" type="set" field="status" rank="0" style="background-color:#ccc;">取消加精</span> -->
          </div>
          <span class="fly-list-nums">
            <!-- 评论数 -->
            <a href="#comment"><i class="iconfont" title="回答">&#xe60c;</i> {$Post.comment}</a>
            <!-- 访问次数 -->
            <i class="iconfont" title="人气">&#xe60b;</i> {$visitCount}
          </span>
        </div>
        <div class="detail-about">
          <a class="fly-avatar" href="{:url('home/user/index')}">
            <img src="{$Post.user.avatar ? $Post.user.avatar : '/static/home/res/images/avatar/default.png'}" alt="贤心">
          </a>
          <div class="fly-detail-user">
            <a href="{:url('home/user/index')}" class="fly-link">
              <cite>{$Post.user.nickname}</cite>
              <i class="iconfont icon-renzheng" title="认证信息：{$Post.user.nickname}"></i>

              {if condition="$Post.user.vip > 0"}
              <i class="layui-badge fly-badge-vip">VIP{$Post.user.vip}</i>
              {/if}
            </a>
            <!-- 发布时间 -->
            <span>{$Post.createtime}</span>
          </div>
          <!-- 帖子按钮 -->
          <div class="detail-hits" id="LAY_jieAdmin" data-id="123">
            <!-- 积分 -->
            <span style="padding-right: 10px; color: #FF7200">悬赏：{$Post.point}积分</span>

            <!-- 登录的用户 和 发帖人的id相等 -->
            {if condition="$Think.cookie.LoginUser['id'] == $Post.user.id"}
            <span class="layui-btn layui-btn-xs jie-admin" type="edit"><a
                href="{:url('home/post/edit',['postid' => $Post.id])}">编辑此贴</a></span>
            {/if}
             <!-- 收藏 -->
             {if condition="$Think.cookie.LoginUser"}
             {if condition="$CollectStatus"}
             <span class="layui-btn layui-btn-xs jie-admin layui-bg-black" type="edit" id="CollectDel">
               <a href="javascript:void(0)">取消收藏</a>
             </span>
             {else /}
             <span class="layui-btn layui-btn-xs jie-admin layui-bg-blue" type="edit" id="CollectAdd">
               <a href="javascript:void(0)">收藏</a>
             </span>
             {/if}
             {/if}
          </div>
        </div>
        <div class="detail-body photos">
          {$Post.content}
        </div>
      </div>
      <!-- 回帖 -->
      <div class="fly-panel detail-box" id="flyReply">
        <fieldset class="layui-elem-field layui-field-title" style="text-align: center;">
          <legend>回帖</legend>
        </fieldset>
        {if condition="$commentList"}
        <ul class="jieda" id="jieda">
          {:CommentHtml($commentList,$Post)}
          <!-- 无数据时 -->
          <!-- <li class="fly-none">消灭零回复</li> -->
        </ul>
        {else/}
        <div class="fly-none" style="min-height: 60px;">消灭零回复</div>
        {/if}
        {if condition="$Think.cookie.LoginUser and !$Post.accept"}
        <div id="comment" class="layui-form layui-form-pane">
          <form action="{:url('home/comment/add')}" method="post">
            <div class="layui-form-item">
              <!-- 回复昵称 -->
              <h2 id="reply-nickname"></h2>
            </div>

            <div class="layui-form-item layui-form-text">
              <div class="layui-input-block">
                <!-- 富文本编辑器 -->
                <textarea id="content" name="content" placeholder="请输入内容" class="layui-textarea"
                  style="height: 300px;"></textarea>
              </div>
            </div>

            <div class="layui-form-item">
              <!-- 这条评论的上一级 -->
              <input type="hidden" name="pid" value="0" />
              <!-- 帖子id -->
              <input type="hidden" name="postid" value="{$Post.id}" />
              <button type="submit" class="layui-btn">提交回复</button>
            </div>
          </form>
        </div>
        {/if}
      </div>
    </div>
    <div class="layui-col-md4">
      <!-- 本周热议 -->
      <dl class="fly-panel fly-list-one">
        <dt class="fly-panel-title">本周热议</dt>
        {foreach $comhot as $item}
        {if $item.commentpost}
        <dd>
          <a href="{:url('home/index/info', ['postid' => $item['postid']])}" target="_blank">{$item.commentpost}</a>
          <span><i class="iconfont icon-pinglun1"></i> {$item.p}</span>
        </dd>
        {/if}
        {/foreach}
        <!-- 无数据时 -->
        <!--
          <div class="fly-none">没有相关数据</div>
          -->
      </dl>
      <!-- 微信扫码关注公众号 -->
      <div class="fly-panel" style="padding: 20px 0; text-align: center;">
        <img src="/static/home/res/images/weixin.jpg" style="max-width: 100%;" alt="layui">
        <p style="position: relative; color: #666;">微信扫码关注 layui 公众号</p>
      </div>

    </div>
  </div>
</div>
<script>
  layui.use(['layer'], function () {
    var $ = layui.jquery,
      layer = layui.layer;

    //添加收藏
    $('#CollectAdd').on('click', function () {
      //发送异步请求
      $.ajax({
        url: `{:url('home/collect/add')}`,
        type: 'post',
        data: `postid={$Post.id}`,
        dataType: 'json',
        success: function (success) {
          if (success.result) {
            //成功
            layer.msg(success.msg, {
              icon: 1,
              time: 2000 //2秒关闭（如果不配置，默认是3秒）
            }, function () {
              //直接刷新界面
              location.reload()
            })
          } else {
            //失败
            layer.msg(success.msg)
          }
        }
      })
    })

    //取消收藏
    $('#CollectDel').on('click', function () {
      //发送异步请求
      $.ajax({
        url: `{:url('home/collect/del')}`,
        type: 'post',
        data: `postid={$Post.id}`,
        dataType: 'json',
        success: function (success) {
          if (success.result) {
            //成功
            layer.msg(success.msg, {
              icon: 1,
              time: 2000 //2秒关闭（如果不配置，默认是3秒）
            }, function () {
              //直接刷新界面
              location.reload()
            })
          } else {
            //失败
            layer.msg(success.msg)
          }
        }
      })
    })

    // 删除帖子
    $('#PostDel').click(function () {
      // 当前对象变成全局
      var that = $(this);
      layer.confirm('确认删除该帖子？', {
        icon: 3,
        title: '删除'
      }, function (index) {
        // console.log();
        var id = that.data('id');
        $.ajax({
          type: "post",
          url: `{:url('home/post/del')}`,
          data: {
            id: id
          },
          dataType: "json",
          success: function (response) {
            // console.log(response);
            if (response.code == 1) {
              layer.msg(response.msg, {
                icon: 1
              }, function () {
                location.href = `{:url('home/index/index')}`;
                return false;
              })
            } else {
              layer.msg(response.msg, {
                icon: 2
              })
            }
          }
        });
        // 关闭窗口
        layer.close(index);
      })

    });

    //点赞
    $('.jieda-zan').click(function () {
      // var postid = "{$Post.id}";
      var comid = $(this).data('id');

      // 判断是否有这个类名 如果有就移除，没有就添加
      // $('.jieda-zan').toggleClass('zanok');
      if ($(this).hasClass('zanok')) {
        var num = Number($(this).find('em').text()) - 1;
        $(this).find('em').text(num);
        $(this).removeClass('zanok');
      } else {
        var num = Number($(this).find('em').text()) + 1;
        $(this).find('em').text(num);
        $(this).addClass('zanok');
      }

      $.ajax({
        type: 'post',
        url: `{:url('home/comment/like')}`,
        data: {
          comid: comid
        },
        dataType: "json",
        success: function (res) {
          if (res.code == 1) {
            layer.msg(res.msg, {
              icon: 6
            });
          } else if (res.code == 2) {
            layer.msg(res.msg, {
              icon: 5
            });
          } else {
            layer.msg(res.msg, {
              icon: 2
            });
          }
        }
      })
    });

    //删除评论回复
    $('.delcomment').on('click',function(){
      var pid = $(this).attr('data-pid');
      layer.confirm('是否确认删除', {
        icon: 3,
        title: '提示'
      }, function (index) {

        //关闭弹框
        layer.close(index)

        //发送异步请求
        $.ajax({
          url: `{:url('home/comment/del')}`,
          type: 'post',
          data: `pid=` + pid,
          dataType: 'json',
          success: function (success) {

            if (success.result) {
              //成功
              layer.msg(success.msg, {
                icon: 1,
                time: 2000 //2秒关闭（如果不配置，默认是3秒）
              }, function () {
                //直接刷新界面
                location.reload()
              })
            } else {
              //删除失败
              layer.msg(success.msg)
            }
          }
        })
      })
    })

    //添加评论回复
    $('.reply').on('click', function () {

      // 获取回复的昵称
      // $("input[name='']")
      var nickname = $(this).data('nickname');
      $('#reply-nickname').html('回复@' + nickname);
      var pid = $(this).data('pid');

      $('input[name="pid"]').val(pid);

      // 富文本编辑器产生光标
      editor.focus();
    })

    // 采纳
    $('.accept').click(function () {
      // var comid = $(this).data('comid');
      var that = $(this);
      layer.confirm('是否采纳该条评论？', {
        icon: 3,
        title: '采纳'
      }, function (index) {
        var comid = that.data('comid');
        // 异步请求
        $.ajax({
          type: 'post',
          url: `{:url('home/comment/accept')}`,
          data: {
            comid: comid
          },
          dataType: 'json',
          success: function (res) {
            if (res.code == 1) {
              layer.msg(res.msg, {
                icon: 1
              }, function () {
                location.reload();
                return false;
              })
            } else {
              layer.msg(res.msg, {
                icon: 2
              })
            }
          }
        })

        // 关闭窗口
        layer.close(index);
      });

    })
  })
</script>